
// window.onload = function(){
//     setTimeout(function(){
//     var loader = document.getElementsByClassName("loader")[0];
//     loader.className="loader fadeout" ;//使用渐隐的方法淡出loading page
//     setTimeout(function(){loader.style.display="none"},1000)
//     },1000)//强制显示loading page 1s
// }
$(document).ready(function () {
    flag = 1;
    showNav();
    $('#content').hide();
});

$('.btn-my').click(function () {
    $('.btn-my').attr("class", "btn-my");
    $(this).attr("class", "btn-my active-btn");
});

function showNav(){
    $.get("api.php?action=3", function (data, status) {
        var obj = JSON.parse(data);
        var items = $('#funitem');
        for (x in obj['data']) {
            items.append('<div class="col-6 col-sm-6 col-md-4 col-lg-3"><div id="blockItem" class="card-my bg3 p-2 block-item" nid="' + obj.data[x].id + '"><i class="bi bi-x-circle block-del my-hide"></i><a href="' + obj.data[x].url + '" class="m-0" target="_blank">' + obj.data[x].name + '</a></div></div>');
        }
        items.append('<div class="col-6 col-sm-6 col-md-4 col-lg-3"><div class="card-my bg3 p-2 block-item" data-toggle="modal" data-target="#exampleModal" ><h6 class="m-0">+ 添加书签</h6></div></div>');
        items.append('<div class="col-6 col-sm-6 col-md-4 col-lg-3"><div id="del" class="card-my bg3 p-2 block-item"><h6 class="m-0">- 删除书签</h6></div></div>');
    });
}

function getSearch() {
    var searchName = $(".active-btn").attr("id");
    return searchName;
}

function getSearchValue() {
    var searchValue = document.getElementById("values").value;
    return searchValue;
}

function showMsg(msg, code) {
    if (code == 1) {
        $('#notice').attr("class", "color-green");
    } else {
        $('#notice').attr("class", "color-red");
    }
    $('#msg').html(msg);
    $('#liveToast').toast('show');
}

function selectX(){
    if(flag%2==1){
        $('#blockItem>i').attr('class',"bi bi-x-circle block-del my-hide");
        $('#del>h6').html("- 删除书签");
    }else{
        $('#blockItem>i').attr('class',"bi bi-x-circle block-del");
        $('#del>h6').html("- 退出");
    }

}
$('#add').click(function () {
    var names = document.getElementById("names").value;
    var urls = document.getElementById("urls").value;
    $.post("api.php?action=4",
        {
            names: names,
            urls: urls
        },
        function (data, status) {
            var obj = JSON.parse(data);
            showMsg(obj.msg, obj.code);
            if (obj.code == 1)
                setTimeout("location.reload()", "1000")
        });
});
$(".search-btn").click(function () {
    switch (getSearch()) {
        case "1":
            window.open("https://www.baidu.com/s?wd=" + getSearchValue());
            break;
        case "2":
            window.open("https://www.google.com.hk/search?q=" + getSearchValue());
            break;
        case "3":
            window.open("https://www.bing.com/search?q=" + getSearchValue());
            break;
        case "4":
            window.open("https://www.sogou.com/web?query=" + getSearchValue());
            break;
    }

});

$('#login').click(function () {

    var name = document.getElementById("name").value;
    var pass = document.getElementById("pass").value;
    $.post("api.php?action=1",
        {
            uname: name,
            upasswd: pass
        },
        function (data, status) {
            var obj = JSON.parse(data);
            showMsg(obj.msg, obj.code);
            if (obj.code == 1)
                setTimeout("location.reload()", "1000")
        });
});

$('#logout').click(function () {

    $.get("api.php?action=2", function (data, status) {
        var obj = JSON.parse(data);
        showMsg(obj.msg, obj.code);
        setTimeout("location.reload()", "1000")
    });
});



$('#values').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        $("#searchBtn").click();

    }
});
$('#names,#urls').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        $("#add").click();

    }
});
$('#name,#pass').bind('keypress', function (event) {
    if (event.keyCode == "13") {
        $("#login").click();
       
    }
});

$("#funitem").on('click','.block-del',function(e){
    var id = $(this).parent().attr('nid')
    $.post("api.php?action=6",
        {
            id:id
        },
        function (data, status) {
            var obj = JSON.parse(data);
            showMsg(obj.msg, obj.code);
            if (obj.code == 1)
                {
                    $('#funitem').html("");
                    showNav();
                }
        });
    setTimeout("selectX()","500");
        
});
$("#funitem").on('click','#del',function(e){
    flag++;
    selectX();

});
